package com.kvest.sqlitetest;

import android.app.Activity;
import android.content.Intent;
import android.content.UriMatcher;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.kvest.sqlitetest.contentprovider.ProductContentProvider;
import com.kvest.sqlitetest.datastorage.sqlitestorage.TestHelper;

import java.util.List;

public class ContentProviderActivity extends Activity {
    private static TestHelper testHelper;

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.content_provider_activity);

        testHelper = new TestHelper(this);
    }

    public static TestHelper getHelper() {
        return testHelper;
    }

    public void onShowAllProductsClick(View view) {
        Intent intent = new Intent(this, ProductsListActivity.class);
        intent.putExtra(ProductsListActivity.STORAGE_TYPE_EXTRA, ProductsListActivity.STORAGE_TYPE_SIMPLE);
        startActivity(intent);
    }

    public void onShowAllProductsMultithreadClick(View view) {
        Intent intent = new Intent(this, ProductsListActivity.class);
        intent.putExtra(ProductsListActivity.STORAGE_TYPE_EXTRA, ProductsListActivity.STORAGE_TYPE_MULTITHREAD);
        startActivity(intent);
    }

    public void onClearProductsClick(View view) {
        Log.d("KVEST_TAG", "deleted count=" +
              getContentResolver().delete(ProductContentProvider.ProductContentProviderMetadata.PRODUCTS_URI, null, null));
    }

    public void onUriMatcherTestClick(View view) {
        testUriMatcher();
    }

    public void onShowDynamicListClick(View view) {
        startActivity(new Intent(this, DynamicListActivity.class));
    }

    public void onGetTypeTestClick(View view) {
        Uri uri1 = ProductContentProvider.ProductContentProviderMetadata.PRODUCTS_URI;
        Uri uri2 = Uri.withAppendedPath(uri1, "5");
        Log.d("KVEST_TAG", uri1 + "=" + getContentResolver().getType(uri1));
        Log.d("KVEST_TAG", uri2 + "=" + getContentResolver().getType(uri2));
    }

    public void onShowCursorLoaderListClick(View view) {
        startActivity(new Intent(this, CursorLoaderListActivity.class));
    }

    private void testUriMatcher() {
        Uri[] uris = new Uri[6];
        uris[0] = Uri.parse("http://google.com/test/123");
        uris[1] = Uri.parse("content://test/people");
        uris[2] = Uri.parse("content://test/people/2");
        uris[3] = Uri.parse("content://test/people/id/Kvest");
        uris[4] = Uri.parse("content://test/people/id/Kvest/state");
        uris[5] = Uri.parse("content://test/people/id/Kvest/state/good");

        UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI("google.com", "test/#", 1);
        matcher.addURI("test", "people", 2);
        matcher.addURI("test", "people/#", 3);
        matcher.addURI("test", "people/id/*", 4);
        matcher.addURI("test", "people/id/*/state", 5);
        matcher.addURI("test", "people/id/*/state/*", 6);

        for (Uri uri : uris) {
            int id = matcher.match(uri);
            Log.d("KVEST_TAG", "id=" + id);
            List<String> segments = uri.getPathSegments();
            for (int j = 0; j < segments.size(); ++j) {
                Log.d("KVEST_TAG", "[" + j + "]=" + segments.get(j));
            }
            Log.d("KVEST_TAG", "last=" + uri.getLastPathSegment());
        }
    }
}
